package com.lucanjun.cn.springsecurity;

import com.lucanjun.cn.springsecurity.common.constant.EnvConstant;
import com.lucanjun.cn.springsecurity.common.utils.DateUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.core.env.Environment;

import java.io.IOException;
import java.io.InputStream;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

/**
 * @author lucanjun
 * @create 2025-03-10
 * @describe 启动类
 */
@Slf4j
@SpringBootApplication
public class MainApplication {

    public static String appStartTime = "";
    public static String appBuildTime = "";
    public static String appExtIp = "";

    public static void main(String[] args) {
        SpringApplication application = new SpringApplication(MainApplication.class);

        //set default profile
        Map<String, Object> defProperties = new HashMap<>();
        defProperties.put("spring.profiles.default", EnvConstant.LOCAL);
        application.setDefaultProperties(defProperties);

        Environment env = application.run(args).getEnvironment();
        //log env info
        logApplicationStartup(env);

        InputStream is = application.getClass().getClassLoader().getResourceAsStream("META-INF/MANIFEST.MF");

        Properties prop = new Properties();
        try {
            prop.load(is);
        } catch (IOException ex) {
            log.error(ex.toString());
        }
        appBuildTime = prop.getProperty("appBuildTime");

    }

    private static void logApplicationStartup(Environment env) {
        String protocol = "http";
        if (env.getProperty("server.ssl.key-store") != null) {
            protocol = "https";
        }
        String serverPort = env.getProperty("server.port");
        String contextPath = env.getProperty("server.servlet.context-path");
        if (StringUtils.isBlank(contextPath)) {
            contextPath = "/";
        }
        String hostAddress = "localhost";
        try {
            hostAddress = InetAddress.getLocalHost().getHostAddress();
        } catch (UnknownHostException e) {
            log.warn("The host name could not be determined, using `localhost` as fallback");
        }
        log.info("\n----------------------------------------------------------\n\t" +
                        "Application '{}' is running! Access URLs:\n\t" +
                        "Local: \t\t{}://localhost:{}{}\n\t" +
                        "External: \t{}://{}:{}{}\n\t" +
                        "Actuator: \t{}://{}:{}{}/actuator\n\t" +
                        "Swagger: \t{}://{}:{}{}/doc.html\n\t" +
                        "Profile(s): \t{}\n----------------------------------------------------------",
                env.getProperty("spring.application.name"),
                protocol,
                serverPort,
                contextPath,
                protocol,
                hostAddress,
                serverPort,
                contextPath,
                protocol,
                hostAddress,
                serverPort,
                contextPath,
                protocol,
                hostAddress,
                serverPort,
                contextPath,
                env.getActiveProfiles());

        // 设置启动时间
        MainApplication.appStartTime = DateUtil.toTime(new Date());
        // 设置ip
        MainApplication.appExtIp = hostAddress;
    }

}
